﻿using System.Collections.Generic;

namespace ttlang.builder
{

    public struct BuildLog
    {
        public string desc;
        public string id;//错误编码 like CS3021 这种统一的错误号
    }
    public enum BuildState
    {
        NONE,//还没编译
        SUCC,//成功
        FAIL,//发现错误
    }

    public enum BuildTarget
    {
        CSharp,
        C,
        TypeScript,
        JavaScript,
    }
    public class ProjectResult
    {
        public ttlang.builder.Result[] targets;
        public int targetcount;
        public int succcount;
        public override string ToString()
        {
            return "succ(" + succcount + "/" + targetcount + ")";
        }
    }

    public class Result
    {
        public Result(string outputpath, string temppath)
        {
            this.outputpath = outputpath;
            this.temppath = temppath;
        }
        public string outputpath;
        public string temppath;
        public string autobuild;
        public BuildTarget target;
        public Dictionary<string, int> outputFiles = new Dictionary<string, int>();
        public BuildState state;
        public List<BuildLog> logs = new List<BuildLog>();
        public void WriteFile(string filename, string code)
        {
            var allfilename = System.IO.Path.Combine(outputpath, filename);
            if (System.IO.Directory.Exists(outputpath) == false)
                System.IO.Directory.CreateDirectory(outputpath);
            if (System.IO.File.Exists(allfilename))
                System.IO.File.Delete(allfilename);
            var bytes = System.Text.Encoding.UTF8.GetBytes(code);
            System.IO.File.WriteAllBytes(allfilename, bytes);

            outputFiles[filename] = bytes.Length;
        }
    }
    public interface IBuilder
    {
        string Name
        {
            get;
        }
        BuildTarget type
        {
            get;
        }
        Result BuildNode(node.Node node, out string result);
        Result BuildModule(semantic.Module node, string outputpath, string temppath, ProjectOption.ProjectType outputType, bool autoBuild, Newtonsoft.Json.Linq.JObject buildOption);
    }
}